package com.dz.lm.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.util.FileUploadUtil;

/**
 * 功能：存放控制器常用功能，为业务功能提供公共接口方法
 * 
 * @author zhanghairong
 * @since 2019-11-16
 */
public abstract class Basic2Control {
	public Logger logger = LogManager.getLogger();
	public static final String RETCODE = "retCode";
	public static final String RETMSG = "retMsg";
	public static final String RESULT = "result";
	public static final String MESSAGE = "message";
	public static final String SUCCESS_MSG = "SUCCESS";
	public static final String SUCCESS_CODE = "000000";
	
	
	/**
	 * 返回值加密
	 * 
	 **/
	protected Map<String, Object> parameterPackage(HttpServletRequest request, JSONObject subjo) {
		logger.info("Return value of Process:" + subjo);
		if (subjo == null) {
			subjo = new JSONObject();
		}
		Map<String, Object> data = new HashMap<>();
		data.put("_resData", subjo.toString());
		Map<String, Object> map = new HashMap<>();
		map.put(RETCODE, SUCCESS_CODE);
		map.put(RETMSG, SUCCESS_MSG);
		map.put("retData", data);
		return map;
	}
	
	/**
	 * 修改图片，图片位置变换的处理办法
	 * @param imgName 图片名称前缀
	 * @param param 参数列表
	 * @param maxImgCnt 最大图片数量
	 **/
	protected void checkImage(String imgName, Map<String, Object> param, int maxImgCnt) {
		for (int cnt = 1; cnt <= maxImgCnt; cnt++) {
			if (param.get(imgName + cnt) == null) {
				param.put(imgName + cnt, "");
			}
		}
	}
	
	/**
	 * 删除无效图片
	 * @param deleteFiles 图片url结合，用逗号分隔
	 **/
	protected void deleteImages(Object deleteFiles) {
		String files[] = null;
		boolean deleteFlag = false;
		if (deleteFiles != null && !"".equals(deleteFiles.toString())) {
			files = deleteFiles.toString().split(",");
			for (String file : files) {
				deleteFlag = FileUploadUtil.deleteFile(file);
				if (deleteFlag) {
					logger.info(file + "File's delete is Success!");
				} else {
					logger.info(file + "File's delete is Fail!");
				}
			}
		}
	}
}
